package com.cumt.controller.home.impl;

import com.cumt.controller.home.UserController;
import com.cumt.entity.User;
import com.cumt.framework.context.ServiceBeanContext;
import com.cumt.framework.dubbo.DubboBaseInterface;
import com.cumt.framework.exception.DubboProviderException;
import com.cumt.framework.rest.impl.BaseRestServerInterfaceImpl;
import com.cumt.provider.UserProvider;
import com.cumt.tool.kafka.KafkaProducerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * Created by caicf on 2016/12/27.
 */
@Controller
@RequestMapping("/user")
public class UserControllerImpl extends BaseRestServerInterfaceImpl<User> implements UserController {

    @Autowired
    @Qualifier("userProvider")
    private UserProvider userProvider;

    @Override
    public DubboBaseInterface getDubboBaseInterface() {
        return userProvider;
    }

    private KafkaProducerUtil kafkaProducerUtil = KafkaProducerUtil.getInstance();

    @Override
    public ModelAndView login() {
        ModelAndView mv = new ModelAndView("login");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("                              login");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        return mv;
    }

    @Override
    public String getName() {

        try {
            String requestURI = request().getRequestURI();
            /*
             *  Kafka测试
             */
//            kafkaProducerUtil.sendMessage("cumt2", 1, "sdf", "我想ni了!");

            /*
             * WebSocket测试
             */
//            WsSessionUtil.broadcast("hhhhhhhhhhhhhhhhhhhhhh");
//            userProvider.sayHello("sdf");

            jsonView().successPack(requestURI);
        } catch (DubboProviderException e) {
            jsonView().failPack(e);
        }

        return toJsonStr(jsonView());
    }

    @Override
    public String save() {
        try {
            UserProvider userProvider = ServiceBeanContext.getInstance().getBean("userProvider", UserProvider.class);
            jsonView().successPack(userProvider.findById(1), request().getRequestURI());
        } catch (DubboProviderException e) {
            e.printStackTrace();
            jsonView().failPack("error");
        }
        return toJsonStr(jsonView());
    }

}
